/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.itver.gui.visual;

import java.awt.BorderLayout;
import java.util.logging.Logger;
import org.itver.arm.models.elements.Arm;
import org.itver.graphics.model.EnvironmentLimits;
import org.itver.graphics.model.MainSceneComponent;
import org.itver.graphics.model.Universe;
import org.itver.graphics.util.ComponentType;
import org.itver.graphics.view.MainSceneComponentEditor;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.windows.Mode;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.itver.gui.visual//PropertiesWindow//EN",
autostore = false)
public final class PropertiesWindowTopComponent extends TopComponent {

    private static PropertiesWindowTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    private static final String PREFERRED_ID = "PropertiesWindowTopComponent";
    private MainSceneComponentEditor msc;

    public PropertiesWindowTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(PropertiesWindowTopComponent.class, "CTL_PropertiesWindowTopComponent"));
        setToolTipText(NbBundle.getMessage(PropertiesWindowTopComponent.class, "HINT_PropertiesWindowTopComponent"));
//        setIcon(ImageUtilities.loadImage(ICON_PATH, true));
        putClientProperty(TopComponent.PROP_UNDOCKING_DISABLED, Boolean.TRUE);
        putClientProperty(TopComponent.PROP_KEEP_PREFERRED_SIZE_WHEN_SLIDED_IN, Boolean.TRUE);
        putClientProperty(TopComponent.PROP_MAXIMIZATION_DISABLED, Boolean.TRUE);
        putClientProperty(TopComponent.PROP_DRAGGING_DISABLED, Boolean.TRUE);
        putClientProperty(TopComponent.PROP_CLOSING_DISABLED, Boolean.TRUE);
        msc = new MainSceneComponentEditor();
        add(msc, BorderLayout.NORTH);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
    public static synchronized PropertiesWindowTopComponent getDefault() {
        if (instance == null) {
            instance = new PropertiesWindowTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the PropertiesWindowTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized PropertiesWindowTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(PropertiesWindowTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof PropertiesWindowTopComponent) {
            return (PropertiesWindowTopComponent) win;
        }
        Logger.getLogger(PropertiesWindowTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID
                + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_NEVER;
    }

    @Override
    public void componentOpened() {
        System.out.println("componentOpened");
        for(MainSceneComponent mcs: Universe.getInstance().getScene().getComponentArray()){
            if(mcs.getType()==ComponentType.arm){
                Arm arm = (Arm) mcs.getTransformGroup().getChild(0);
                arm.addListenerToPieces(msc);
            }
                mcs.addPropertyChangeListener(msc);
        }
        EnvironmentLimits.getInstance().addPropertyChangeListener(msc);
    }

    @Override
    public void componentClosed() {
        System.out.println("componentClosed");
        for(MainSceneComponent mcs: Universe.getInstance().getScene().getComponentArray()){
            mcs.removePropertyChangeListener(msc);
        }
        EnvironmentLimits.getInstance().removePropertyChangeListener(msc);
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    Object readProperties(java.util.Properties p) {
        if (instance == null) {
            instance = this;
        }
        instance.readPropertiesImpl(p);
        return instance;
    }

    private void readPropertiesImpl(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    public MainSceneComponentEditor getEditor(){
        return msc;
    }
}
